<!--
 * @Author: your name
 * @Date: 2021-09-07 18:37:49
 * @LastEditTime: 2021-09-09 15:02:44
 * @LastEditors: LAPTOP-JN5U4VA6
 * @Description: In User Settings Edit
 * @FilePath: \ej-webui\src\pages\webSource\category.vue
-->
<template>
  <div class="category">
    <div class="btns">
      <!-- 检索框 -->
      <el-button @click="addHandler" type="primary" size="small" style="margin-right: 10px" >添加</el-button>
    </div>
    <div class="content">
      <!-- 表格 -->
      <el-table :data="resData" style="width: 100%" size="small">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="排序号" prop="no"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="editHandler(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="delHandler(scope.row.id)" style="color: red">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <!-- total表示：数据的总条数  current-page表示：当前所在的页数   -->
      <el-pagination
        background
        layout="prev, pager, next, sizes"
        :total="resData.total"
        :page-size="params.pageSize"
        :current-page="params.page"
        @current-change="pageChangeHandler"
        :page-sizes="[5, 10, 15]"
        @size-change="pageSizeChangeHandler"
      >
      </el-pagination>
      <!-- 模态框 -->
      <el-dialog :title="title" :visible.sync="visible" @close="dialogCloseHandler">
        <el-form  :model="categoryF" :rules="rules" ref="saveForm">
          <el-form-item label="栏目名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="categoryF.name"></el-input>
          </el-form-item>
          <!-- prop 表示的是父子组件之间传参   -->
          <el-form-item label="序号" :label-width="formLabelWidth" prop="no">
            <el-input v-model="categoryF.no"></el-input>
          </el-form-item>
          <el-form-item label="产品简介" :label-width="formLabelWidth">
            <el-input
              type="textarea"
              v-model="categoryF.description"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSaveHandler">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import request from "@/utils/request";
import { mapActions } from "vuex";
import qs from "qs";
import _ from "lodash";

export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 5,
      },
      resData: [],
      title: "",
      categoryF: {},
      visible: false,
      formLabelWidth: '80px',
      rules: {
        name: [
          { required: true, message: "栏目名称不能为空", trigger: "blur"},
        ],
        no: [
          {
            required: true,
            message: "栏目序号不能为空",
            trigger: "blur"
          }
        ],
        description: [
          { required: true, message: "栏目介绍不能为空", trigger: "blur"}
        ],
      }

    };
  },
  methods: {
    // 导入状态机中的方法
    ...mapActions("category", ["findAllCategories"]),
    // 分页查询产品表格
    async pageQueryCategories() {
      // 过滤数据将空字符串删除
      for (let key in this.params) {
        if (this.params[key] == "") {
          delete this.params[key];
        }
      }
      let res = await request.get("/category/findAll", {
        params: this.params,
      });
      this.resData = res.data;
    },
    // page改变事件  
    pageChangeHandler(page) {
      // 将更新了的页码传递给页面   并且刷新查询到的对应页码上的内容
      this.params.page = page;
      this.pageQueryCategories();
    },
    // pageSize改变事件  pageSize是每页中显示的数据条目
    pageSizeChangeHandler(pageSize) {
      this.params.pageSize = pageSize;
      this.pageQueryCategories();
    },

    // 新增按钮  处理函数
    addHandler(){
      this.categoryF = {};
      this.title = '新增栏目信息';
      this.visible = true;
    },
    // 添加完成后的保存事件
    toSaveHandler(){
      this.$refs["saveForm"].validate((valid) => {
        if(valid) {
          request.post("/category/saveOrUpdate", qs.stringify(this.categoryF))
            .then((res) => {
              this.pageQueryCategories(); // 重载数据
              this.$message.success(res.message); // 提示操作结果信息
              this.visible = false; // 关闭模态框
            });
        } else {
          return fasle;
        }
      });
    },

    // 关闭模态框的回调函数
    dialogCloseHandler() {
      // 置空表单验证
      this.$refs["saveForm"].resetFileds();
    },

    // 编辑按钮  事件函数
    editHandler(row) {
      // 将当前行数据赋值给数据模型
      this.categoryF = _.clone(row);
      this.title = "编辑栏目信息";
      this.visible = true;
    },
    // 删除 事件
    delHandler(id) {
      this.$confirm('此操作将永久删除该轮播图, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
          request.get('/category/deleteById',{ params:{id} }).then((res) => {
              // 刷新数据
              this.pageQueryCategories();
              this.$message({
                  type: "success",
                  message: res.message,
              }); // 提示成功信息
          })
      })
    },
  },
  created() {
    this.pageQueryCategories(); // 分页查询产品
    this.findAllCategories()
  },
};
</script>

<style scoped>
/* 图片上传  样式 */
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>